/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Variable;
	ref class Material;
	ref class Geometry;

	/// <summary>
	/// Represents a coupling between a single <see cref="OptixDotNet::Geometry">Geometry</see> node 
	/// and a set of <see cref="OptixDotNet::Material">Materials</see>.
    /// </summary>
	public ref class GeometryInstance : public OptixNode, IContainerNode<Material^>, INodeCollectionProvider<Material^>, IVariableContainer
	{
	public:

		/// <summary>
		/// Creates a GeometryInstance
		/// </summary>
		GeometryInstance( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			CheckError( rtGeometryInstanceValidate( mInstance ) );
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			if( mInstance )
				CheckError( rtGeometryInstanceDestroy( mInstance ) );

			mInstance = 0;
		}

		/// <summary>
		/// Gets or Attaches a material node to a geometry instance at [index]
        /// </summary>
		property NodeCollection<Material^>^ Materials
		{
			virtual NodeCollection<Material^>^ get() = INodeCollectionProvider<Material^>::Collection::get
			{
				return mCollection;
			}
		}

		/// <summary>
		/// Gets or Sets the number of materials to be attached to the geometry instance.
        /// </summary>
		property unsigned int MaterialCount
		{
			unsigned int get()
			{
				unsigned int count;
				CheckError( rtGeometryInstanceGetMaterialCount( mInstance, &count ) );

				return count;
			}
			void set( unsigned int count )
			{
				CheckError( rtGeometryInstanceSetMaterialCount( mInstance, count ) );
			}
		}

		/// <summary>
		/// Gets the Material node object attached to a GeometrInstance node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		Material^ GetMaterial( unsigned int index );

		/// <summary>
		/// Attaches a Material node object to a GeometrInstance node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		/// <param name="material">Material to attach</param>
		void SetMaterial( unsigned int index, Material^ material );

		/// <summary>
		/// Attaches a Material node object to the end of a Geometry node child list
        /// </summary>
		/// <param name="material">Material to add</param>
		void AddMaterial( Material^ material )
		{
			SetMaterial( MaterialCount++, material );
		}

		/// <summary>
		/// Removes a Material from the geometry instance.
        /// </summary>
		/// <param name="material">Material to remove from the GeometryInstance.</param>
		/// <returns>True if a matching material was found, False otherwiese</returns>
		bool RemoveMaterial( Material^ material );

		/// <summary>
		/// Removes a material from the geometry instance at index [index].
        /// </summary>
		/// <param name="index">Index of the material to remove.</param>
		void RemoveMaterialAtIndex( int index )
		{
			if( MaterialCount > 0 )
			{
				RTmaterial temp;
				CheckError( rtGeometryInstanceGetMaterial( mInstance, MaterialCount - 1, &temp ) );
				CheckError( rtGeometryInstanceSetMaterial( mInstance, index, temp ) );
				MaterialCount--;
			}
		}

		/// <summary>
		/// Sets the geometry object for the geometry instance
        /// </summary>
		property OptixDotNet::Geometry^ Geometry
		{
			OptixDotNet::Geometry^ get();
			void set( OptixDotNet::Geometry^ geometry );
		}

		/// <summary>
		/// Gets the number of global variables on the Context
        /// </summary>
		property int VariableCount
		{
			virtual int get()
			{
				unsigned int count;
				CheckError( rtGeometryInstanceGetVariableCount( mInstance, &count ) );
				return count;
			}
		}

		/// <summary>
		/// Gets a local variable on the geometry instance with name [s]. If the variable doesn't exist, it is created.
        /// </summary>
		property Variable^ default[System::String^]
		{
			virtual Variable^ get( System::String^ s );
			virtual void set( System::String^s, Variable^ v );
		}

		/// <summary>
		/// Gets a local geometry instance variable at index [index]
        /// </summary>
		property Variable^ default[int]
		{
			virtual Variable^ get ( int index );
			virtual void set( int index, Variable^ v );
		}

	internal:
		GeometryInstance( Context^ context, RTgeometryinstance instance );
		inline RTgeometryinstance InternalPtr() { return mInstance; }

		/// <summary>
		/// Gets or Attaches a child node to a selector at [index]
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property Material^ ChildNodes[unsigned int]
		{
			virtual Material^ get ( unsigned int index ) = IContainerNode<Material^>::ChildNodes::get;
			virtual void set ( unsigned int index, Material^ node ) = IContainerNode<Material^>::ChildNodes::set;
		}

		/// <summary>
		/// Sets the number of child nodes to be attached to the selector.
        /// </summary>
		property unsigned int ChildCount
		{
			virtual unsigned int get() = IContainerNode<Material^>::ChildCount::get
			{
				return MaterialCount;
			}
			virtual void set( unsigned int count ) = IContainerNode<Material^>::ChildCount::set
			{
				MaterialCount = count;
			}
		}

	private:

		RTgeometryinstance	mInstance;
		NodeCollection<Material^>^ mCollection;
	};
}